﻿using EmbeddedSharp.Core;
using EmbeddedSharp.Scripting;
using Zove.Runtime.Core;
using System.Configuration;

namespace Ascend.Runtime.Core {
    public class AscendLanguageNature : ILanguageNature {
        protected AscendClassFinder AscendClassFinder;
        protected AscendClassParserFactory AscendClassParserFactory;
        protected AscendExpressionParserFactory AscendExpressionParserFactory;

        protected DefaultClassLoader DefaultClassLoader;
        protected DefaultExpressionTranslator DefaultExpressionTranslator;
        protected ContextFunctionManager DefaultFunctionManager;
        protected DefaultRuntimeModelLoader DefaultRuntimeModelLoader;

        #region ILanguageNature Members

        public string[] AtomicTypes {
            get {
                return new[] { "factor", "real", "set", "boolean" };
            }
        }

        public IClassFinder ClassFinder {
            get {
                if (AscendClassFinder == null) {
                    string workspace = ConfigurationManager.AppSettings["ascend workspaces"];

                    string[] workspaces = { };
                    if (!string.IsNullOrEmpty(workspace)) {
                        workspaces = workspace.Split(';');
                    }

                    AscendClassFinder = new AscendClassFinder(workspaces);
                }

                return AscendClassFinder;
            }
        }

        public IClassLoader ClassLoader {
            get {
                if (DefaultClassLoader == null) {
                    DefaultClassLoader = new DefaultClassLoader(this);
                }

                return DefaultClassLoader;
            }
        }

        public IClassParserFactory ClassParserFactory {
            get {
                if (AscendClassParserFactory == null) {
                    AscendClassParserFactory = new AscendClassParserFactory(this);
                }

                return AscendClassParserFactory;
            }
        }

        public IExpressionParserFactory ExpressionParserFactory {
            get {
                if (AscendExpressionParserFactory == null) {
                    AscendExpressionParserFactory = new AscendExpressionParserFactory(this);
                }

                return AscendExpressionParserFactory;
            }
        }

        public IExpressionTranslator ExpressionTranslator {
            get {
                if (DefaultExpressionTranslator == null) {
                    DefaultExpressionTranslator = new DefaultExpressionTranslator();
                }

                return DefaultExpressionTranslator;
            }
        }

        public IFunctionManager FunctionManager {
            get {
                if (DefaultFunctionManager == null) {
                    DefaultFunctionManager = new ContextFunctionManager();
                }

                return DefaultFunctionManager;
            }
        }

        public IRuntimeModelLoader ModelLoader {
            get {
                if (DefaultRuntimeModelLoader == null) {
                    DefaultRuntimeModelLoader = new DefaultRuntimeModelLoader(this);
                }

                return DefaultRuntimeModelLoader;
            }
        }

        #endregion
    }
}